<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>日志管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                    <div class="layui-input-inline">
                        用户姓名：<input type="text" class="layui-input userVal" placeholder="请输入用户姓名"/>
                    </div>
                    <div class="layui-input-inline">
                        IP地址：<input type="text" class="layui-input ipVal" placeholder="请输入IP地址"/>
                    </div>
                    <div class="layui-input-inline">
                        结果：<input type="text" class="layui-input resultVal" placeholder="请输入结果"/>
                    </div>
                    <div class="layui-input-inline">
                        开始时间：<input type="text" class="layui-input startVal" placeholder="请输入开始时间"/>
                    </div>
                    <div class="layui-input-inline">
                        结束时间：<input type="text" class="layui-input endVal" placeholder="请输入结束时间"/>
                    </div>
                                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
            </div>
        </form>
    </blockquote>
    <table id="logtable" lay-filter="logtable"></table>
    <!--操作-->
    <script type="text/html" id="logDealBar">
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="del">删除</a>
    </script>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['form', 'layer', 'table', 'laytpl'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table;
        //用户列表
        var tableIns = table.render({
            elem: '#logtable',
            url: 'logSelectByPage.do',
            cellMinWidth: 95,
            page: true,
            height: "full-200",
            limits: [10, 15, 20, 25],
            limit: 10,
            id: "logtablezz",
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'id', title: '编号', minWidth: 100, align: "center", templet: function (obj) {
                        return obj.id;
                    }
                },
                {field: 'ip', title: 'IP', minWidth: 100, align: "center", templet: function (obj) {
                        return obj.ip;
                    }
                },
                {field: 'user', title: '用户', align: 'center', templet: function (obj) {
                        return obj.user;
                    }
                },
                {field: 'type', title: '类型', align: 'center', templet: function (obj) {
                        return obj.type;
                    }
                    },
                {
                    field: 'createtime', title: '创建日期', align: 'center', minWidth: 90, templet: function (obj) {
                        return obj.createtime;
                    }
                },
                {field: 'body', title: '内容', align: 'center', templet: function (obj) {
                        return obj.body;
                    }
                },
                {field: 'result', title: '结果', align: 'center', templet: function (obj) {
                        return obj.result;
                    }
                },
                {title: '操作',  templet: '#logDealBar', fixed: "right", align: "center"}
            ]]
        });
        //实现搜索功能
        $(".search_btn").click(function () {
            var user = $(".userVal").val();
            var ip=$(".ipVal").val();
            var result=$(".resultVal").val();
            var begin=$(".startVal").val();
            var end=$(".endVal").val();
            table.reload('logtablezz', {
                where: {
                    user:user,
                    ip:ip,
                    result:result,
                    begin:begin,
                    end:end
                }
            });
        });

        //tool(xx)这里的xx是和上边表个的lay-filter="userList"对应的
        table.on("tool(logtable)", function (obj) {
            //获取当前行的数据
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    deleteUser(data);
                    break;
            }
        });

        //删除方法
        function deleteUser(data) {
            layer.confirm('您确定要删除【' + data.user + '】这个用户吗？', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    url: "deleteLog.do",
                    data: {id: data.id},
                    type: "delete",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            //刷新表格
                            layer.msg(res.msg);
                            tableIns.reload();
                        } else if (res.code == -1) {
                            layer.msg(res.msg);
                        }
                    }
                });
                //关闭提示框
                layer.close(index);
            });
        }

       // 批量删除
        $(".delAll_btn").click(function () {
            var checkStatus = table.checkStatus('logtablezz');
            var data = checkStatus.data;
            var uIds = [];
            if (data.length > 0) {
                for (var i in data) {
                    uIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: "deleteManyLog.do",
                        type: "delete",
                        data: {ids: uIds},
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg);
                                tableIns.reload();
                            }
                        }
                    })
                    layer.close(index);
                })
            } else {
                layer.msg("请选择需要删除的用户");
            }
        });
    });

</script>

</body>
</html>